﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace information
{
    public partial class AlterData : Form
    {
        public AlterData()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            //数据输入,判断无空输入
            if (textBox3.Text.Length ==0)
            {
                MessageBox.Show("ID无输入");
                textBox3.Focus();
            }
            else if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("姓名无输入");
                textBox1.Focus();
            }
            else if (textBox2.Text.Length == 0)
            {
                MessageBox.Show("性别无输入");
                textBox2.Focus();
            }
            else
            {  
                //数据库连接
                string strcon;
                strcon = "Server=192.168.0.88; Database=information;User id ='sa';pwd=123456";  //information            
                SqlConnection myon = new SqlConnection(strcon);
                myon.Open();
                MessageBox.Show("数据库连接成功", "数据库");

                //接受控件数据
                int id = Convert.ToInt32(textBox3.Text);;
                string s1 = textBox1.Text, s2 = textBox2.Text;

                //写入数据库
                try
                {   
                    SqlCommand sqlcom = new SqlCommand("update InformationTable set name='" + s1 + "',sex='" + s2 + "'" + "where id=" + id, myon);
                    sqlcom.ExecuteNonQuery();
                    MessageBox.Show("修改成功！", "啊");
                    myon.Close();
                }
                catch (Exception a)
                {
                    MessageBox.Show(a.ToString());
                }
            }
           }

        //e对象中的属性KeyChar获取ID的按键值
        private void IDKeyPress(object sender, KeyPressEventArgs e)
        {
            if ((!char.IsDigit(e.KeyChar)) && e.KeyChar != 13) //char类中IsDigit方法判断,13为Enter键
            {
                MessageBox.Show("ID只能输入数字", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }

    }
}
